const sqlite3 = require("sqlite3").verbose()
const path = require("path")
const GenId = require("../utils/SnowFlake")

// let db = new sqlite3.Database(__dirname + "/")
let db = new sqlite3.Database(path.join(__dirname, "blog.sqlite3"))
const genid = new GenId({ WorkerId: 1 })

db.async = {}

db.async.all = (sql, params) => {//封装进一个Promise,执行查询语句，使用all方法
  return new Promise((resolve, reject) => {
    db.all(sql, params, (err, rows) => {
      resolve({ err, rows })
    })
  })
}


db.async.run = (sql, params) => {//执行插入、修改、删除语句，使用run方法,封装进一个Promise
  return new Promise((resolve, reject) => {
    db.run(sql, params, (err, rows) => {
      resolve({ err, rows })
    })
  })
}


module.exports = { db, genid }